﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB;

namespace GlxRRSMain.UserControls
{
    public partial class CORevenueManagementControl : UserControl
    {
        private CinemaCORevenueViewTableAdapter x_CinemaBORevenueViewTableAdapter = new CinemaCORevenueViewTableAdapter();
        private List<ReportDataSet.CinemaCORevenueViewRow> x_ListCinemaCORevenue = null;
        ReportDataSet.CinemaCORevenueViewRow x_CinemaCORevenue = null;
        //
        private tblCinemaTableAdapter x_tblCinemaTableAdapter = new tblCinemaTableAdapter();
        private List<ReportDataSet.tblCinemaRow> x_ListCinema = null;
        //
        private tblCORevenueTableAdapter x_tblCORevenueTableAdapter = new tblCORevenueTableAdapter();

        //
        private void CORevenueManagementControl_Load(object sender, EventArgs e)
        {
            initDataGridView();
            //
            gridCORevenue.DataSource = ListCinemaCORevenue;
            //
            cbCinema.DataSource = ListCinema;
            cbCinema.DisplayMember = "CinemaName";
        }
        public CORevenueManagementControl()
        {
            InitializeComponent();
        }
        private void initDataGridView()
        {
            initCORevenueDataGridView();
        }
        private void initCORevenueDataGridView()
        {
            gridCORevenue.AutoGenerateColumns = false;
            gridCORevenue.ReadOnly = true;

            DataGridViewTextBoxColumn c01 = new DataGridViewTextBoxColumn();
            c01.DataPropertyName = "CORevenueID";
            c01.HeaderText = "CORevenue ID";

            DataGridViewTextBoxColumn c02 = new DataGridViewTextBoxColumn();
            c02.DataPropertyName = "CinemaName";
            c02.HeaderText = "Cinema Name";

            DataGridViewTextBoxColumn c04 = new DataGridViewTextBoxColumn();
            c04.DataPropertyName = "CO";
            c04.HeaderText = "CO Column";

            DataGridViewTextBoxColumn c05 = new DataGridViewTextBoxColumn();
            c05.DataPropertyName = "Date";
            c05.HeaderText = "CORevenue Date Column";
            //
            gridCORevenue.Columns.Add(c01);
            gridCORevenue.Columns.Add(c02);
            gridCORevenue.Columns.Add(c04);
            gridCORevenue.Columns.Add(c05);
        }

        private void bindingCORevenueData()
        {
            if (CurrentCinemaCORevenue != null)
            {
                txtCORevenueID.Text = CurrentCinemaCORevenue.CORevenueID.ToString();
                cbCinema.Text = CurrentCinemaCORevenue.CinemaName;
                dpickerDateCORevenue.Value = CurrentCinemaCORevenue.Date;
                txtCO.Text = CurrentCinemaCORevenue.CO.ToString();
            }
        }

        public List<ReportDataSet.CinemaCORevenueViewRow> ListCinemaCORevenue
        {
            get
            {
                if (x_ListCinemaCORevenue == null)
                {
                    x_ListCinemaCORevenue = x_CinemaBORevenueViewTableAdapter.GetData().ToList();
                }
                return x_ListCinemaCORevenue;
            }
            set
            {
                x_ListCinemaCORevenue = value;
            }
        }

        public ReportDataSet.CinemaCORevenueViewRow CurrentCinemaCORevenue
        {
            get
            {
                if (x_CinemaCORevenue == null)
                {
                    if (gridCORevenue.CurrentRow != null)
                    {
                        x_CinemaCORevenue = ListCinemaCORevenue[gridCORevenue.CurrentRow.Index];
                    }
                }
                return x_CinemaCORevenue;
            }
            set
            {
                x_CinemaCORevenue = value;
            }
        }

        public List<ReportDataSet.tblCinemaRow> ListCinema
        {
            get
            {
                if (x_ListCinema == null)
                {
                    x_ListCinema = x_tblCinemaTableAdapter.GetData().ToList();
                }
                return x_ListCinema;
            }
            set
            {
                x_ListCinema = value;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtCO.Text = "";
            dpickerDateCORevenue.Value = DateTime.Now;
        }

        private void gridCORevenue_SelectionChanged(object sender, EventArgs e)
        {
            x_CinemaCORevenue = null;
            bindingCORevenueData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int CinemaID = ((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem).ID;
            long BOTarget = long.Parse(txtCO.Text);
            DateTime FromDate = dpickerDateCORevenue.Value;
            //
            ReportDataSet.tblTargetDataTable t = new ReportDataSet.tblTargetDataTable();
            x_tblCORevenueTableAdapter.Insert(CinemaID, BOTarget, FromDate);

            ListCinemaCORevenue = null;
            gridCORevenue.DataSource = ListCinemaCORevenue;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int targetID = int.Parse(txtCORevenueID.Text);
            //
            ReportDataSet.tblCORevenueRow current = x_tblCORevenueTableAdapter.GetData()
                .Where(kvp => kvp.ID == targetID)
                .First();
            //
            int CinemaID = ((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem).ID;
            long COTarget = long.Parse(txtCO.Text);
            DateTime FromDate = dpickerDateCORevenue.Value;
            //
            current.CinemaID = CinemaID;
            current.CO = COTarget;
            //
            current.Date = dpickerDateCORevenue.Value;

            x_tblCORevenueTableAdapter.Update(current);

            ListCinemaCORevenue = null;
            gridCORevenue.DataSource = ListCinemaCORevenue;
        }
    }
}
